#include "ConstDefine.h"
#include "CGlobal.h"
b2Vec2 pt2Vec( const cocos2d::CCPoint &pt )
{
	b2Vec2 vec;
	vec.x=pt.x/PTM_RATIO;
	vec.y=pt.y/PTM_RATIO;
	return vec;
}

cocos2d::CCPoint vec2Pt( const b2Vec2 &vec )
{
	cocos2d::CCPoint pt;
	pt.x=vec.x*PTM_RATIO;
	pt.y=vec.y*PTM_RATIO;
	return pt;
}

float distanceP2P( const cocos2d::CCPoint &pt1,const cocos2d::CCPoint &pt2 )
{
	return sqrt((pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y) );
}

b2Vec2 getPhysicsSpeed( float fDis,float fSec,cocos2d::CCPoint ptDirection )
{
	/*fDis=47.099022;
	fSec=0.049992;
	ptDirection.x=-11.000000;
	ptDirection.y=0.000000;*/

	float fXPositive=ptDirection.x>0.0f?ptDirection.x:(-1*ptDirection.x);
	float fYPositive=ptDirection.y>0.0f?ptDirection.y:(-1*ptDirection.y);
	float fV=fDis/fSec/PTM_RATIO;

	float fVX=fV;
	float fVY=fV;
	if (fXPositive==0.0||fYPositive==0.0)
	{
		if(fXPositive==0.0)
		{
			fVX=fV;
			fVY=0.0;
		}
		if (fXPositive==0.0)
		{
			fVX=0.0;
			fVY=fV;
		}
	}
	else
	{
		float fVUnit=sqrt((fV*fV)*(fXPositive+fYPositive)*(fXPositive+fYPositive)/((fXPositive*fYPositive)+(fXPositive*fYPositive)));
		fVX=fVUnit*fXPositive/(fXPositive+fYPositive);
		fVY=fVUnit*fYPositive/(fXPositive+fYPositive);
	}
	
	

	fVX=ptDirection.x>=0?fVX:(-1*fVX);
	fVY=ptDirection.y>=0?fVY:(-1*fVY);
	
	
	/*char buf[1024];
	sprintf(buf,"(%f,%f),(%fs,%f),(%f,%f)\n",fVX,fVY,fSec,fDis,ptDirection.x,ptDirection.y);
	OutputDebugStringA(buf);*/
	return b2Vec2(fVX,fVY);

}

float getMinScale()
{
	return CGlobal::GetRatioX()>CGlobal::GetRatioY()?CGlobal::GetRatioY():CGlobal::GetRatioX();
}